<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的实践报名 - 企业培训平台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        /* --- 全局布局与变量 (已美化) --- */
        :root {
            --sidebar-width: 240px;
            --primary-color: #007bff;
            --danger-color: #dc3545;
            --success-color: #28a745;
            --secondary-color: #6c757d;
            --light-gray: #f8f9fa;
            --border-color: #dee2e6;
            --text-color: #343a40;
            --card-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            --card-shadow-hover: 0 8px 15px rgba(0, 0, 0, 0.1);
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #f4f5f7;
            margin: 0;
            color: var(--text-color);
        }
        .main-content {
            margin-left: var(--sidebar-width);
            padding: 30px;
        }
        .container-card {
            background-color: #ffffff;
            border-radius: 12px;
            box-shadow: var(--card-shadow);
            padding: 30px 35px;
            transition: box-shadow 0.3s ease;
        }
        .container-card:hover {
            box-shadow: var(--card-shadow-hover);
        }
        .page-header {
            margin: 0 0 30px 0;
            padding-bottom: 20px;
            border-bottom: 1px solid var(--border-color);
            font-size: 2em;
            font-weight: 600;
        }

        /* --- 动态提示框 (带图标和关闭按钮) --- */
        .alert {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            margin-bottom: 25px;
            border-radius: 8px;
            border: 1px solid transparent;
            font-size: 0.95em;
            position: relative;
        }
        .alert i { margin-right: 12px; font-size: 1.2em; }
        .alert-success { background-color: #e9f7ef; color: #1d643b; border-color: #b7e4c7; }
        .alert-danger { background-color: #fdeded; color: #8a1818; border-color: #f7c5c5; }

        /* --- 现代化表格 --- */
        .table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.95em;
        }
        .table th, .table td {
            padding: 15px;
            text-align: left;
            vertical-align: middle;
            border-bottom: 1px solid var(--border-color);
        }
        .table thead th {
            background-color: var(--light-gray);
            font-weight: 600;
            color: #495057;
        }
        .table tbody tr {
            transition: background-color 0.25s ease;
        }
        .table tbody tr:hover {
            background-color: #f5f5f5;
        }
        .table tbody tr:last-child td {
            border-bottom: none;
        }

        /* --- 状态徽章和按钮 (已美化) --- */
        .status-badge {
            padding: 5px 12px;
            border-radius: 20px;
            font-weight: 600;
            font-size: 0.8em;
        }
        .status-enrolled {
            color: var(--success-color);
            background-color: rgba(40, 167, 69, 0.1);
        }
        .status-canceled {
            color: var(--secondary-color);
            background-color: rgba(108, 117, 125, 0.1);
            text-decoration: none;
        }
        .btn {
            display: inline-block;
            font-weight: 600;
            text-align: center;
            cursor: pointer;
            border: 1px solid transparent;
            padding: 8px 18px;
            font-size: 0.9em;
            border-radius: 6px;
            transition: all 0.2s ease-in-out;
            text-decoration: none;
            color: #fff;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .btn-danger {
            background-color: var(--danger-color);
            border-color: var(--danger-color);
        }

        /* --- 空状态提示 (已美化) --- */
        .info-box {
            text-align: center;
            padding: 50px 30px;
            color: #777;
            background-color: #fafbfd;
            border: 1px dashed #d1d9e2;
            border-radius: 12px;
            margin-top: 20px;
        }
        .info-box i { font-size: 3em; color: #a0aec0; margin-bottom: 20px; }
        .info-box p { font-size: 1.1em; margin: 0; }

        /* --- 响应式设计 --- */
        @media (max-width: 768px) {
            .table thead { display: none; }
            .table tr { display: block; margin-bottom: 20px; border-radius: 8px; border: 1px solid var(--border-color); box-shadow: var(--card-shadow); }
            .table td { display: flex; justify-content: space-between; align-items: center; padding: 12px 15px; border-bottom: 1px solid var(--border-color); text-align: right; }
            .table td:last-child { border-bottom: none; }
            .table td::before { content: attr(data-label); font-weight: 600; text-align: left; padding-right: 15px; }
        }
    </style>
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>

<div class="main-content">
    <div class="container-card">
        <h1 class="page-header">我的实践报名</h1>

        <div th:if="${successMessage}" class="alert alert-success">
            <i class="fas fa-check-circle"></i> <span th:text="${successMessage}"></span>
        </div>
        <div th:if="${errorMessage}" class="alert alert-danger">
            <i class="fas fa-times-circle"></i> <span th:text="${errorMessage}"></span>
        </div>

        <div th:if="${!#lists.isEmpty(enrollments)}">
            <table class="table">
                <thead>
                <tr>
                    <th>实践活动标题</th><th>所属课程</th><th>实践地点</th>
                    <th>实践时间</th><th>报名状态</th><th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="enrollment : ${enrollments}">
                    <td class="practice-title" data-label="活动标题" th:text="${enrollment.practiceTitle}"></td>
                    <td data-label="所属课程" th:text="${enrollment.courseName}"></td>
                    <td data-label="实践地点" th:text="${enrollment.practiceLocation}"></td>
                    <td data-label="实践时间" th:text="${#temporals.format(enrollment.practiceTime, 'yyyy-MM-dd HH:mm')}"></td>
                    <td data-label="报名状态">
                        <span th:if="${enrollment.status == 'ENROLLED'}" class="status-badge status-enrolled">已报名</span>
                        <span th:if="${enrollment.status == 'CANCELED'}" class="status-badge status-canceled">已取消</span>
                    </td>
                    <td data-label="操作">
                        <form th:if="${enrollment.status == 'ENROLLED'}" class="cancel-form" th:action="@{/student/practice/my-enrollments/cancel/{id}(id=${enrollment.id})}" method="post">
                            <button type="submit" class="btn btn-danger">取消报名</button>
                        </form>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <div th:if="${#lists.isEmpty(enrollments)}" class="info-box">
            <i class="fas fa-info-circle"></i>
            <p>您目前没有已报名的线下实践活动。</p>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 选取所有取消报名的表单
        document.querySelectorAll('.cancel-form').forEach(form => {
            // 监听它们的提交事件
            form.addEventListener('submit', function(event) {
                // 1. 阻止表单立即提交
                event.preventDefault();

                // 2. 从表单行中获取活动标题
                const row = form.closest('tr');
                const title = row.querySelector('.practice-title').innerText;

                // 3. 使用SweetAlert2弹出更美观的确认框
                Swal.fire({
                    title: '您确定要取消报名吗?',
                    text: `您将要取消 “${title}”`,
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#dc3545', // 红色确认按钮
                    cancelButtonColor: '#6c757d',  // 灰色取消按钮
                    confirmButtonText: '是的，确认取消',
                    cancelButtonText: '再想想'
                }).then((result) => {
                    // 4. 如果用户点击了“确认”按钮
                    if (result.isConfirmed) {
                        // 5. 手动提交原始表单，功能流程不变
                        form.submit();
                    }
                });
            });
        });
    });
</script>

</body>
</html>